package com.zxw.article.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxw.article.service.ArticleService;
import com.zxw.common.api.ApiResult;
import com.zxw.common.api.PageResult;
import com.zxw.common.constant.ApiConst;
import com.zxw.common.entity.article.ArticleListQuery;
import com.zxw.common.entity.article.ArticleQuery;
import com.zxw.common.entity.article.ArticleVO;
import com.zxw.common.enums.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 文章信息表接口
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-09-19
 */
@Api(tags = "文章信息表接口", description = "ArticleController")
@RestController
@RequestMapping(ApiConst.API_PREFIX +"/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @ApiOperation("添加")
    @PostMapping("/save")
    public ApiResult<ResultCode> save(@RequestBody ArticleQuery articleQuery) {
        articleService.save(articleQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("列表条件分页查询")
    @GetMapping("/pageList")
    public ApiResult<PageResult<ArticleVO>> pageList(ArticleListQuery articleListQuery) {
        IPage<ArticleVO> iPage = articleService.pageList(articleListQuery);
        return ApiResult.success(PageResult.restPage(iPage, iPage.getRecords()));
    }

    @ApiOperation("批量添加")
    @PostMapping("/saveBatch")
    public ApiResult<ResultCode> saveBatch(@RequestBody List<ArticleQuery> list) {
        articleService.saveBatch(list);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ApiResult<ResultCode> update(@RequestBody ArticleQuery articleQuery) {
        articleService.updateById(articleQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("批量删除")
    @PostMapping("/deletes")
    public ApiResult<ResultCode> deletes(@RequestBody List<String> ids) {
        articleService.removeByIds(ids);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("详情")
    @GetMapping("/detail")
    public ApiResult<ArticleVO> detail(String id) {
        ArticleVO articleVO = articleService.detail(id);
        return ApiResult.success(articleVO);
    }

    @ApiOperation("条件查询")
    @GetMapping("/queryList")
    public ApiResult<List<ArticleVO>> queryList(ArticleListQuery articleListQuery) {
        List<ArticleVO> list = articleService.queryList(articleListQuery);
        return ApiResult.success(list);
    }


    @ApiOperation("查询浏览排行")
    @GetMapping("/queryScanRankList")
    public ApiResult<List<ArticleVO>> queryScanRankList(int limit) {
        List<ArticleVO> list = articleService.queryScanRankList(limit);
        return ApiResult.success(list);
    }

    @ApiOperation("Echarts饼图：文章分类统计")
    @GetMapping("/echartsArticleCategoryCountPie")
    public ApiResult<List<Map<String, Object>>> echartsArticleCategoryCountPie() {
        List<Map<String, Object>> list = articleService.echartsArticleCategoryCountPie();
        return ApiResult.success(list);
    }


}

